package com.stu.fifthtry.service;

import com.stu.fifthtry.exception.DeleteNotAllowedException;
import com.stu.fifthtry.mapper.SpecializedCoursesMapper;
import com.stu.fifthtry.mapper.StudentMapper;
import com.stu.fifthtry.mapper.SubjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stu.fifthtry.pojo.Subject;
@Service
@Slf4j
public class SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private SpecializedCoursesMapper specializedCoursesMapper;

    public Subject getByName(String name){

        return subjectMapper.getByName(name);
    }

    public void delete(String name) {
        if(!studentMapper.getBySubject(name).isEmpty()){
            throw new DeleteNotAllowedException("已有学生选择");
        }
        subjectMapper.delete(name);
        Subject subject = subjectMapper.getByName(name);
        specializedCoursesMapper.delete(subject.getId());

    }

    public void save(Subject subject) {

        subjectMapper.save(subject);

    }

    public void update(Subject subject) {
        subjectMapper.update(subject);

    }
}
